<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * User_model short summary.
 *
 * User_model description.
 *
 * @version 1.0
 * @author GALAXY
 */
class User_model extends CI_Model
{
    public $user_id;
    public $user_account;
    public $user_pass;
    public $user_gender;
    public $user_name;
    public $user_tel;
    public $user_phone;
    public $user_admin;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    //插入新用户
    public function inset_user($userModel)
    {
        return $this->db->insert('user',$userModel);
    }

    //由用户id 获取用户
    public function get_user_by_id($suerId)
    {
        return $this->db->select()->from('user')->where(array('user_id'=>$suerId))->get()->row();
    }

    //获取所有用户
    public function get_alluser()
    {
        return $this->db->select()->from('user')->get()->result();
    }

    //获取超级管理员
    public function get_super_user()
    {
        return $this->db->select()->from('user')->where(array('user_admin'=>1))->get()->row();
    }

    //更新用户
    public function edit_user($userModel)
    {
        $data=array('user_account'=>$userModel->user_account,'user_pass'=>$userModel->user_pass,'user_gender'=>$userModel->user_gender,'user_name'=>$userModel->user_name,'user_tel'=>$userModel->user_tel,'user_phone'=>$userModel->user_phone);
        return $this->db->update('user',$data,array('user_id'=>$userModel->user_id));
        //return $this->db->affected_rows();
    }

    //删除用户
    public function del_user_byid($user_id)
    {
        return $this->db->delete('user',array('user_id'=>$user_id));
        //return $this->db->affected_rows();
    }

    //批量删除用户
    public function batchdel_user($user_ids)
    {
        $this->db->trans_begin();
        foreach($user_ids as $useriditem)
        {
            $this->db->delete('user',array('user_id'=>$useriditem));
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    public function CheckAddUserName($val)
    {
        $res= $this->db->select()->from('user')->where(array('user_account'=>$val))->get()->row();
        if(empty($res))
        {
            return 1;
        }else{
            return 2;
        }
    }
    public function CheckEditUserName($val)
    {
        $res= $this->db->select()->from('user')->where(array('user_account'=>$val))->get()->result_array();
        if(empty($res) || count($res) <=1)
        {
            return 1;
        }else{
            return 2;
        }
    }



    //查询登陆用户
    public function get_login_user($userModel)
    {
        return $this->db->select()->from('user')->where(array('user_account'=>$userModel->user_account,'user_pass'=>$userModel->user_pass))->get()->result();
    }

    /**
     * @param mixed $cpage
     * @param mixed $pagesize
     * @param mixed $files
     * @param mixed $tabel
     * @param mixed $order
     */
    public function get_page($cpage,$pagesize,$files,$tabel,$order,$temp_id,$where)
    {
        $sql="SELECT $files FROM $tabel a JOIN (select $temp_id from $tabel $where limit ".($cpage-1)*$pagesize.", $pagesize) b ON a.$temp_id = b.$temp_id ";
        $sql_count="select count('$temp_id') as _count from $tabel $where";
        $data['objlist']=$this->db->query($sql)->result();
        $data['count']=$this->db->query($sql_count)->result();
        return $data;
    }
}